'''
Created on Jul 6, 2010

@author: riche
'''
import unittest
from ft.framework import component

class myComp(component.Implementation):
    
    def runTest(self, msg):
        #print(msg)
        self.send_out('out', msg)

class Test(unittest.TestCase):


    def setUp(self):
        self.testComp = myComp('testComponent')
        port = component.Port('testPort', 'runTest')
        port_out = component.Port('out', '__OUT__') 
        self.testComp.add_port(port)
        self.testComp.add_port(port_out)

    def tearDown(self):
        pass

    def test_list_size(self):
        '''Make sure the return list size is correct'''
        self.testComp.run_component('testPort', 'TEST TEST')
        ret_list = self.testComp.get_enqueued_msgs()
        self.assertEqual(len(ret_list), 1)
        
    def test_message(self):
        '''Make sure the message is not changed by the framework'''
        self.testComp.run_component('testPort', 'TEST TEST')
        ret_list = self.testComp.get_enqueued_msgs()
        msg_tuple = ret_list.pop()
        self.assertEqual('TEST TEST', msg_tuple[1])

    def test_exit_port(self):
        '''Make sure the exit port is stored in the output tuple'''
        self.testComp.run_component('testPort', 'TEST TEST')
        ret_list = self.testComp.get_enqueued_msgs()
        msg_tuple = ret_list.pop()
        self.assertEqual('out', msg_tuple[0])
        
    def test_get_endpoint(self):
        '''Return an endpoint given a port name'''
        endp = self.testComp.get_endpoint('testPort')
        self.assertEqual(endp.get_name(), 'testComponent:testPort')
        
    def test_get_bad_endpoint(self):
        '''Returns an error given a bad port name'''
        self.assertRaises(component.UnknownPortError, self.testComp.get_endpoint, 'fail_port')

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testComponent']
    unittest.main()